﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Linq;
using Microsoft.SharePoint;
using CSSoft;
using System.Collections;
using Microsoft.SharePoint.WebControls;

namespace CSSoft.VNTower.Domain
{
    [Serializable]
    public class UserItem
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public SPFieldLookupValue LookupValue { get { return new SPFieldLookupValue(Id, Title); } }
        public string StrLookupValue { get { return LookupValue.ToString(); } }
        //custom fields
        public string Code { get; set; }
        public string JobTitle { get; set; }
        public string Department { get; set; }
        public string Region { get; set; }

        [NonSerialized]
        private SPUser _user;
        public SPUser User { get { return _user; } set { 
            _user = value; 
            Id = _user.ID;
            Title = _user.Name;
            try
            {
                SPList userInfomations = CS2Web.GetRootList(Constants.LIST_USER_INFOMATION);
                 SPQuery query = new SPQuery();
                 query.Query = String.Format(@"<Where>
                         <Eq>
                            <FieldRef Name='User' LookupId='TRUE' />
                            <Value Type='User'>{0}</Value>
                         </Eq>
                   </Where>", Id);
                 SPListItemCollection items = userInfomations.GetItems(query);
                 if (CS2SPQueryBuilder.HasItems(items))
                 {
                     SPListItem item = items[0];
                     Code = item.Title;
                     JobTitle = CS2Convert.ToString(item["JobTitle"]);
                     Department = CS2Convert.ToString(item["Department"]);
                     Region = CS2Convert.ToString(item["Region"]);
                 }
            }
            catch { }
            ////Custom fields
            //Code = Id.ToString();
            //JobTitle = CS2Convert.ToString(htEntityData["JobTitle"]);
            //Department = CS2Convert.ToString(htEntityData["Department"]);
        } }

        public static ArrayList GetEntitiesArrayList(List<UserItem> userItems)
        {
            ArrayList arrayList = new ArrayList();
            if (userItems.Count > 0)
            {
                foreach (UserItem user in userItems)
                {
                    PickerEntity pickerEntity = new PickerEntity();
                    pickerEntity.Key = user.User.LoginName;
                    pickerEntity.IsResolved = true;
                    arrayList.Add(pickerEntity);
                }
            }
            return arrayList;
        }
    }
    public class UserInfomationList
    {
        public static List<UserItem> SiteUsers{get;set;}
        public static bool IsLoadUsers { get { return !(SiteUsers == null || SiteUsers.Count == 0); } }
        public UserInfomationList() { }
        public UserInfomationList(SPWeb web)
        {
            foreach (SPUser item in web.SiteUsers)
                SiteUsers.Add(new UserItem { User = item });
            UserInfomationList.SiteUsers = SiteUsers;
        }
    }
}
